﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;

namespace EBA.MQ
{
    /// <summary>
    /// 消息队列配置节点
    /// <example>
    /// <eba>
    /// <mq localport="">
    ///     <queues>
    ///        <queue name="email" id="xm" >
    ///           <bindings>
    ///             <binding address="tcp:=//192.168.102.1:5555" />
    ///             <binding address="tcp:=//192.168.102.1:5505" />
    ///           </bindings>
    ///        </queue>
    ///     </queues>
    ///     <routes>
    ///         <route name="sms">
    ///           <bindings>
    ///             <binding address="tcp:=//192.168.102.1:5555" />
    ///             <binding address="tcp:=//192.168.102.1:5505" />
    ///           </bindings>
    ///         </route>
    ///     </routes>
    /// </mq>
    /// </eba>
    /// </example>
    /// </summary>
    public class MessageQueueConfigurationSection : ConfigurationSection
    {
        /// <summary>
        /// MQ队列服务节点
        /// </summary>
        [ConfigurationProperty("queues", IsRequired = true)]
        public QueuesConfigurationElementCollection Queues
        {
            get
            {
                return (QueuesConfigurationElementCollection)this["queues"];
            }
            set
            {
                this["queues"] = (object)value;
            }
        }

        /// <summary>
        /// 路由节点
        /// </summary>
        [ConfigurationProperty("routes", IsRequired = true)]
        public RoutesConfigurationElementCollection Routes
        {
            get
            {
                return (RoutesConfigurationElementCollection)this["routes"];
            }
            set
            {
                this["routes"] = (object)value;
            }
        }

        /// <summary>
        /// 本地监听端口
        /// </summary>
        [ConfigurationProperty("localport", IsRequired = true)]
        public string Localport
        {
            get
            {
                return (string)this["localport"];
            }
            set
            {
                this["localport"] = (object)value;
            }
        }
    }

    /// <summary>
    /// 队列节点集合
    /// </summary>
    [ConfigurationCollection(typeof(QueueConfigurationElement), AddItemName = "queue", CollectionType = ConfigurationElementCollectionType.BasicMap)]
    public class QueuesConfigurationElementCollection : ConfigurationElementCollection
    {
        /// <summary>
        /// 创建元素
        /// </summary>
        /// <returns></returns>
        protected override ConfigurationElement CreateNewElement()
        {
            return new QueueConfigurationElement();
        }

        /// <summary>
        /// 获得元素
        /// </summary>
        /// <param name="element"></param>
        /// <returns></returns>
        protected override object GetElementKey(ConfigurationElement element)
        {
            return (QueueConfigurationElement)element;
        }
    }

    /// <summary>
    /// 路由节点集合
    /// </summary>
    [ConfigurationCollection(typeof(QueueConfigurationElement), AddItemName = "route", CollectionType = ConfigurationElementCollectionType.BasicMap)]
    public class RoutesConfigurationElementCollection : ConfigurationElementCollection
    {
        /// <summary>
        /// 创建
        /// </summary>
        /// <returns></returns>
        protected override ConfigurationElement CreateNewElement()
        {
            return new RouteConfigurationElement();
        }

        /// <summary>
        /// 获得
        /// </summary>
        /// <param name="element"></param>
        /// <returns></returns>
        protected override object GetElementKey(ConfigurationElement element)
        {
            return (RouteConfigurationElement)element;
        }

        public RouteConfigurationElement Get(string name)
        {
            return (RouteConfigurationElement)this.BaseGet(name);
        }
    }

    /// <summary>
    /// 绑定信息集合
    /// </summary>
    [ConfigurationCollection(typeof(QueueConfigurationElement), AddItemName = "binding", CollectionType = ConfigurationElementCollectionType.BasicMap)]
    public class BindingsConfigurationElementCollection : ConfigurationElementCollection
    {
        /// <summary>
        /// 创建
        /// </summary>
        /// <returns></returns>
        protected override ConfigurationElement CreateNewElement()
        {
            return new BindingConfigurationElement();
        }

        /// <summary>
        /// 获得
        /// </summary>
        /// <param name="element"></param>
        /// <returns></returns>
        protected override object GetElementKey(ConfigurationElement element)
        {
            return (BindingConfigurationElement)element;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="element"></param>
        public void Add(ConfigurationElement element)
        {
            this.BaseAdd(element);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="element"></param>
        public void Remove(ConfigurationElement element)
        {
            this.BaseRemove(element);
        }

        public BindingConfigurationElement Get(string name)
        {
            return (BindingConfigurationElement)this.BaseGet(name);
        }
    }

    /// <summary>
    /// 队列元素
    /// </summary>
    public class QueueConfigurationElement : ConfigurationElement
    {
        [ConfigurationProperty("bindings", IsRequired = true)]
        public BindingsConfigurationElementCollection Bindings
        {
            get
            {
                return (BindingsConfigurationElementCollection)this["bindings"];
            }
            set
            {
                this["bindings"] = (object)value;
            }
        }

        [ConfigurationProperty("name", IsRequired = true)]
        public string Name
        {
            get
            {
                return (string)this["name"];
            }
            set
            {
                this["name"] = (object)value;
            }
        }

        [ConfigurationProperty("id", IsRequired = true)]
        public string Id
        {
            get
            {
                return (string)this["id"];
            }
            set
            {
                this["id"] = (object)value;
            }
        }
    }

    /// <summary>
    /// 路由元素
    /// </summary>
    public class RouteConfigurationElement : ConfigurationElement
    {
        [ConfigurationProperty("bindings", IsRequired = true)]
        public BindingsConfigurationElementCollection Bindings
        {
            get
            {
                return (BindingsConfigurationElementCollection)this["bindings"];
            }
            set
            {
                this["bindings"] = (object)value;
            }
        }

        [ConfigurationProperty("name", IsRequired = true)]
        public string Name
        {
            get
            {
                return (string)this["name"];
            }
            set
            {
                this["name"] = (object)value;
            }
        }
    }

    /// <summary>
    /// 绑定元素
    /// </summary>
    public class BindingConfigurationElement : ConfigurationElement
    {
        [ConfigurationProperty("address", IsRequired = true)]
        public string Binding
        {
            get
            {
                return (string)this["address"];
            }
            set
            {
                this["address"] = (object)value;
            }
        }
    }
}
